<?php

/*****************************************************************************
 * 
 *  Copyright 2009 Sylvain Dudoit (contact : sylvain.dudoit at gmail.com)
 *  
 *  This file is part of Poupitchi.
 *
 *  Poupitchi is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Poupitchi is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Poupitchi.  If not, see <http://www.gnu.org/licenses/>.
 *
 *****************************************************************************/

require_once("library/php4compatibility.php");

/**
 * Echo shortly
 * @param $string
 * @return unknown_type
 */
function e($string) {
    echo($string);
}


/**
 * Translate
 * @param $string
 * @return unknown_type
 */
function tr($string) {
		global $LANG;
		if (P_LANG != "en") {
			if (isset($LANG[$string])) {
				return $LANG[$string];
			} else {
				file_put_contents("languages/todo.".P_LANG.".php", '"'.$string.'" => "",', FILE_APPEND);
			}
		}
  	return $string;
}


/**
 * Translate and echo
 * @param $string
 * @return unknown_type
 */
function etr($string) {
    e(tr($string));
}


/**
 * Error log
 */
function eError() {
  global $P_ERROR;
  
  if (isset($P_ERROR)) {
    e('<div id="bloc_error" onclick="Effect.toggle(\'bloc_error\',\'appear\'); return false;">'.$P_ERROR.'</div>');
  }
}

/**
 * Set Error
 */
function sError($message) {
  global $P_ERROR;
  
  $P_ERROR .= $message."<br/>";
}

/**
 * Echo user face if exists
 */
function eFace($login) {
  global $P_DATA;
  
  $file = $P_DATA."users/faces/".$login.".".P_FACE_EXT;
  if (file_exists($file)) {
    e('<img src="'.$file.'" alt="'.ucfirst($login).'"  title="'.ucfirst($login).'" border="0" class="face" />');
  } else {
  	e('<span class="face" title="'.ucfirst($login).'">'.wordwrap(ucfirst($login), 10, '<br/>').'</span>');
  }
}

/**
 * Print pagination
 */
function ePagination($start, $total, $nbitems) {
    global $P_MODULE;
    
    e('<div id="pagination"><ul>');
    if (($start-$nbitems) >= 0) {
    	e('<li><a href="index.php?action=show&module='.$P_MODULE.'&start='.($start-$nbitems).'"> '
    	 .'&lt;&lt; '.tr("Next").'</a></li>');
    }
    if ((($total-$start) > $nbitems)) {
    	e('<li><a href="index.php?action=show&module='.$P_MODULE.'&start='.($start+$nbitems).'"> '
    	 .tr("Previous").' &gt;&gt;</a></li>');
    }
    e('</ul></div>');
}

/**
 * Print image
 * @return unknown_type
 */
function ePhoto($file) {
    global $P_DATA;

    echo '<a href="'.$P_DATA.'photos/'.$file.'" target="_blank">';
    if (file_exists("cache/thumbs/".$file)) {
        echo '<img src="cache/thumbs/'.$file.'" border="0" class="photo" />'; 
    } else {
        echo '<img src="library/externals/thumb/thumb.display.php?img=../../../'.$P_DATA.'/photos/'.$file.'" border="0" class="photo" />'; 
    } 
    echo '</a>';
}


/**
 * Print Look bloc
 */
function eLookBloc() {
		global $P_MODULES;
		global $P_MODULE;
		
		echo ('<div id="bloc_look">');
		foreach($P_MODULES as $module) {
        if ($P_MODULE != $module) {
   	        include("library/modules/".$module."/bloc_look.php");
        }
		}
    echo ('</div>');
}


/**
 * Replace http link
 * 
 * @return the given string with anchor included
 */
function textToHtml($s) {
    $links = array();
    $strings = explode(" ", $s);
    foreach($strings as $string) {
        $strings2 = explode("\n", $string);
        foreach($strings2 as $string2) {
            $string2 = trim($string2);
            if (strcmp(substr(strtolower($string2), 0, 7), "http://") == 0) {
                if (!in_array($string2, $links)) {
                    $links[] = $string2;
                }
            }
        }    
    }
    foreach($links as $link) {
        $replace = "<a href=\"".$link."\" target=\"_blank\" >".substr($link, 0, 20)."...</a>";
        $s = str_replace($link, $replace, $s);
    }
    return nl2br($s);
}


/**
 * Serialize data
 */
function serializeWithMask(&$data) {
    return "<?php //".base64_encode(serialize($data))." ?>";
}


/**
 * Unserialize data
 */
function unserializeWithMask($data) {
    return unserialize(base64_decode(substr($data, 8, -3)));
}




/**
 * Return the list of files/dirs of a path
 * and remove "." dir and file
 * @param $path
 * @return an array 
 */
function getDirList($path, $number=-1, $start=0) {
    $files = scandir($path, 1);
    foreach($files as $k => $v){
        if ((substr($v, 0, 1) == ".") || ($v == "index.html")) {
            unset($files[$k]);
        } elseif (is_dir($path."/".$v)) {
            unset($files[$k]);
        }
    }
    if ($number != -1) {
        $files = array_slice($files, $start, $number);
    } else {
        $files = array_slice($files, $start);
    }
    return $files;
}


/**
 * Get a file name from a login
 */
function getFileName($login) {
    $filename = date(P_FILE_DATE, time()).$login.".php";
    sleep(1);
    return $filename;
}


/**
 * Secure string
 */
function secstr($string) {
    return str_replace('\\', '', str_replace('"', "'", htmlentities($string, ENT_QUOTES, 'UTF-8')));
}


/**
 * Check file name
 */
function checkFilename($filename) {
		if ((strpos($filename, '/') !== false)  
				|| (strpos($filename, "\\") !== false)  
				|| (substr($filename, 0, 1) == ".")
				|| ($filename === "index.html") 
				|| ($filename === "adminpass.php"))  {
				return false;
		}
		return true;
}


?>